/**
 *
gcc -shared ane.c dbs.c words.c shellsort.c -L. -lFlashRuntimeExtensions -l"sqlite3" -o "..\my.ane\META-INF\ANE\Windows-x86/test.dll" -Wl,--kill-at
gcc -shared ane.c dbs.c words.c shellsort.c -L. -lFlashRuntimeExtensions -l"sqlite3" -o "..\my.ane\META-INF\ANE\Windows-x86/test.dll" -Wl,--kill-at
  */
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include"dbs.h"
#include"words.h"
#include "FlashRuntimeExtensions.h"

FREObject addwords(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
//	int addword(const char* str,int isright);
	uint32_t length; 
	uint32_t isright; 
	const uint8_t* value;
	FREGetObjectAsUTF8((FREObject)argv[0], &length, &value); 
	FREGetObjectAsInt32((FREObject)argv[1],&isright); 
	//printf("(%d)(%s)(%d)",length,value,isright);
	addword(value,isright);
	const uint8_t* msg = (const uint8_t*)"Hello world!!!!";
	FREObject retObj;
	FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);
	return retObj;
}

FREObject initsqlite(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	uint8_t msg[24]="0oooooooooo0\n"; 
	sprintf(msg+5,"%d",initdb());
	FREObject retObj;
	FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);
	return retObj;
}

FREObject selectdb(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
//int getdays(const char*tablename,int start,int end,char*retstr,int* retlen);	
	uint32_t length; 
	uint32_t start; 
	uint32_t end; 
	const uint8_t* value; 
	//const uint8_t* msg = (const uint8_t*)"Hello world!!!!";
	FREGetObjectAsUTF8((FREObject)argv[0], &length, &value); 
	FREGetObjectAsInt32((FREObject)argv[1],&start); 
	FREGetObjectAsInt32((FREObject)argv[2],&end); 
	char retstr[1024*16];
	int retlen;
	getdays(value,start,end,retstr,&retlen);
	FREObject retObj;
	FRENewObjectFromUTF8(strlen((const char*)retstr)+1, retstr, &retObj);
	return retObj;

}

/**
模糊查词
*/
FREObject getwordlist(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	uint32_t len; 
	const uint8_t* name;
	FREGetObjectAsUTF8(argv[0], &len, &name); 
	char retstr[2048]="";
	int strlen;
	findwords(name,retstr,&strlen);
	FREObject retObj;

	FRENewObjectFromUTF8(
			strlen+1,
			retstr,
			&retObj);
	return retObj;
}
/**

*/
FREObject gethistorybyindex(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	uint32_t id; 
	int len; 
	FREGetObjectAsInt32(argv[0],&id); 
	char retstr[2048];
	gethistory(id,(char*)retstr,&len);
	FREObject retObj;
	FRENewObjectFromUTF8(strlen((const char*)retstr)+1, retstr, &retObj);
	return retObj;
}
/**

*/
FREObject sethistorybyindex(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	uint32_t id; 
	uint32_t len; 
	FREGetObjectAsInt32(argv[0],&id); 
	const uint8_t* name;
	FREGetObjectAsUTF8(argv[1], &len, &name); 
	printf("sethistory:id=%d,name=%s:%d\n",id,name,len);
	int i = sethistory(id,name);
	char msg[32];
	sprintf(msg,"sethistory:%d,%s,success:%d\n",id,name,i);
	FREObject retObj;
	FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);
	return retObj;
}

/**

*/
FREObject addwordlib(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	uint32_t id; 
	uint32_t start; 
	uint32_t num; 
	uint32_t len1; 
	uint32_t len2; 
	const uint8_t* url;
	const uint8_t* name;
	FREGetObjectAsInt32(argv[0],&id); 
	FREGetObjectAsUTF8(argv[1], &len1, &name); 
	FREGetObjectAsUTF8(argv[2], &len2, &url); 
	FREGetObjectAsInt32(argv[3],&start); 
	FREGetObjectAsInt32(argv[4],&num); 

	int i = addlib(id,name,url,start,num);
	char msg[32]="kk";
	FREObject retObj;
	//sprintf(msg,"add:%d,%s,%s,%d,%d,success:%d\n",id,name,url,start,num,i);
	FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);
	return retObj;
}
/**

*/
FREObject delwordlib(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	uint32_t id; 
	FREGetObjectAsInt32(argv[0],&id); 
	int i = dellib(id);
	char msg[32];
	sprintf(msg,"del:%d,success:%d\n",id,i);
	FREObject retObj;
	FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);
	return retObj;
}
/**

*/
FREObject findwordlib(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	char msg[2048];
	int retlen;
	//const uint8_t* msg = (const uint8_t*)"findwordlib!!!!";
	findlib(msg, &retlen);

	FREObject retObj;
	FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);
	return retObj;
}

/**
根据单词查找单词记录信息
*/
FREObject getwordbystr(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	uint32_t length; 
	const uint8_t* value;
	FREGetObjectAsUTF8((FREObject)argv[0], &length, &value); 
	
	char retstr[1024];
	int retlen;
	getwordmsg(value,retstr, &retlen);
	
	FREObject retObj;
	FRENewObjectFromUTF8(strlen((const char*)retstr)+1, retstr, &retObj);
	return retObj;
}

FREObject test(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	const uint8_t* msg = (const uint8_t*)"Hello world!!!!";
	FREObject retObj;
	FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);
	return retObj;
}
FREObject getmeanof(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	FREObject retObj;
	uint32_t length; 
	const uint8_t* value;
	const uint8_t* msg; 
	FREGetObjectAsUTF8((FREObject)argv[0], &length, &value); 
	//printf("(%d)(%s)",length,value);
	msg = getmeans((uint8_t*)(value));
	FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);
	//FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);
	return retObj;
}
FREObject getallof(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	FREObject retObj;
	uint32_t length; 
	const uint8_t* value; 
	FREGetObjectAsUTF8(argv[0],&length, &value); 
	static char msg[1024*256]= "hello"; 
	//sprintf(msg,"%s",readf((char*)value,msg,&length));
	int len=0;
	readf((char*)value,msg,&len);
	//sprintf(msg,"%s",(char*)value);
	printf("%s",msg);
	FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);
	return retObj;
}
FREObject closedbs(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	FREObject retObj;
	int i= closedb();
	char msg[1024]= "database"; 
	sprintf(msg+strlen(msg),"%d,closed",i);
	FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);
	return retObj;
}
FREObject getwordmon(FREContext ctx,void*funcData,uint32_t argc,FREObject argv[])
{
	FREObject retObj;
	uint32_t length; 
	uint32_t wordid;
	uint32_t start;
	uint32_t end;

	const uint8_t* value; 
	FREGetObjectAsInt32((FREObject)argv[0],&wordid); 
	FREGetObjectAsInt32((FREObject)argv[1],&start); 
	FREGetObjectAsInt32((FREObject)argv[2],&end); 
	char msg[1024]= "hello"; 
	getwordall(wordid,start,end,msg,(int*)&length);
	//sprintf(msg,"%s",readf((char*)value));
	FRENewObjectFromUTF8(strlen((const char*)msg)+1, msg, &retObj);
	return retObj;
}
void ContextInitializer(void* extData, const uint8_t* ctxType,FREContext ctx,uint32_t* numFunctionsToTest,const FRENamedFunction** functionsToSet)
{
	*numFunctionsToTest= 15;
	FRENamedFunction*func = (FRENamedFunction*)malloc(sizeof(FRENamedFunction)*15);

	func[0].name= (const uint8_t*)"test";
	func[0].functionData= NULL;
	func[0].function= &test;

	func[1].name= (const uint8_t*)"getmeanof";
	func[1].functionData= NULL;
	func[1].function= &getmeanof;

	func[2].name= (const uint8_t*)"getallof";
	func[2].functionData= NULL;
	func[2].function= &getallof;


	func[3].name= (const uint8_t*)"initsqlite";
	func[3].functionData= NULL;
	func[3].function= &initsqlite;

	func[4].name= (const uint8_t*)"addwords";
	func[4].functionData= NULL;
	func[4].function= &addwords;

	func[5].name= (const uint8_t*)"selectdb";
	func[5].functionData= NULL;
	func[5].function= &selectdb;

	
	func[6].name= (const uint8_t*)"closedbs";
	func[6].functionData= NULL;
	func[6].function= &closedbs;

	func[7].name= (const uint8_t*)"getwordmon";
	func[7].functionData= NULL;
	func[7].function= &getwordmon;

	func[8].name= (const uint8_t*)"getwordbystr";
	func[8].functionData= NULL;
	func[8].function= &getwordbystr;

	func[9].name= (const uint8_t*)"findwordlib";
	func[9].functionData= NULL;
	func[9].function= &findwordlib;

	func[10].name= (const uint8_t*)"addwordlib";
	func[10].functionData= NULL;
	func[10].function= &addwordlib;

	func[11].name= (const uint8_t*)"delwordlib";
	func[11].functionData= NULL;
	func[11].function= &delwordlib;

	func[12].name= (const uint8_t*)"sethistorybyindex";
	func[12].functionData= NULL;
	func[12].function= &sethistorybyindex;

	func[13].name= (const uint8_t*)"gethistorybyindex";
	func[13].functionData= NULL;
	func[13].function= &gethistorybyindex;

	func[14].name= (const uint8_t*)"getwordlist";
	func[14].functionData= NULL;
	func[14].function= &getwordlist;
	
	*functionsToSet= func;
}
void ContextFinalizer(FREContext ctx) {}
void ExtInitializer(void** extDataToSet, FREContextInitializer* ctxInitializerToSet,FREContextFinalizer* ctxFinalizerToSet){
	*extDataToSet= NULL;
	*ctxInitializerToSet= ContextInitializer;
	*ctxFinalizerToSet= ContextFinalizer;
}
void ExtFinalizer(void* extData){
 closedb();
}
